<template>
  <div class="app-container">
    <!-- 搜索 -->
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="操作人" prop="operator">
        <el-input
          v-model="queryParams.operator"
          placeholder="请输入操作人名"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="操作类型" prop="operationType">
        <el-select v-model="queryParams.operationType">
          <el-option label="修改密码" value="修改密码"></el-option>
          <el-option label="用户注册" value="用户注册"></el-option>
          <el-option label="修改用户" value="修改用户"></el-option>
          <el-option label="删除用户" value="删除用户"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="时间段">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" style="margin-bottom: 20px">
      <el-col :span="1.5">
        <el-button type="primary" plain size="mini" @click="exportExcel"
          >导出excel</el-button
        >
      </el-col>
    </el-row>
    <!-- 表格数据 -->
    <el-table
      :data="tableData"
      stripe
      v-loading="loading"
      border
      style="margin-bottom: 20px"
      :default-sort="{ prop: 'date', order: 'descending' }"
    >
      <el-table-column
        label="操作人"
        prop="operatorName"
        align="center"
      ></el-table-column>
      <el-table-column
        label="操作类型"
        prop="operationType"
        align="center"
      ></el-table-column>
      <el-table-column
        label="操作目标种类"
        prop="targetType"
        align="center"
      ></el-table-column>
      <el-table-column
        label="操作时间"
        prop="operationTime"
        align="center"
        sortable
      ></el-table-column>
    </el-table>
    <!-- 分页v-show="total > 0" -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryParams.pageCurrent"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="queryParams.pageSize"
      layout="total, sizes, prev, pager, next"
      :total="total"
    ></el-pagination>
  </div>
</template>
<script>
import { log, download } from "@/api/log";
export default {
  data() {
    return {
      // 表格条数
      total: undefined,
      tableData: [],
      loading: false,
      queryParams: {
        pageCurrent: 1,
        pageSize: 10,
        operator: undefined,
      },
      // 日期范围
      dateRange: [],
    };
  },
  methods: {
    // 分页==========================================================
    handleSizeChange(pageSizeVal) {
      this.queryParams.pageSize = pageSizeVal;
      this.getList();
    },
    handleCurrentChange(pageCurrentVal) {
      this.queryParams.pageCurrent = pageCurrentVal;
      this.getList();
    },
    async getList() {
      this.loading = true;
      let res = await log({ ...this.queryParams, dateRange: this.dateRange });
      if (res.code == 200) {
        this.loading = false;
        this.tableData = res.data.records;
        this.total = res.data.total;
      }
    },
    // 导出
    exportExcel() {
      download().then((res) => {
        window.open("http://" + "log/download", "_blank");
        //let blob = new Blob([res], { type: 'application/vnd.ms-excel' })
        //let objectUrl = URL.createObjectURL(blob)
        //window.location.href = objectUrl
      });
    },

    // 搜索
    handleQuery() {
      this.queryParams.pageCurrent = 1;
      this.getList();
    },
    // 重置
    resetQuery() {
      this.dateRange = [];
      this.$refs.queryForm.resetFields();
      this.getList();
    },
  },
  created() {
    this.getList();
  },
};
</script>
